package com.rom.hash;

public class HashTabDemo {

    public static void main(String[] args) {


         HashTab hashTab = new HashTab(7);
         Emp emp = new Emp(1,"xiongjinwang");
         Emp emp1 = new Emp(8,"xiongjinwan");
         Emp emp2 = new Emp(3,"xiongjinwa");
         Emp emp3 = new Emp(4,"xiongjinw");
         Emp emp4 = new Emp(12,"xiongjin");
         Emp emp5 = new Emp(2,"xiongjin");
         Emp emp6 = new Emp(7,"xiongji");
         Emp emp7 = new Emp(15,"xiongji");
         hashTab.add(emp);
         hashTab.add(emp1);
         hashTab.add(emp2);
         hashTab.add(emp3);
         hashTab.add(emp4);
         hashTab.add(emp5);
         hashTab.add(emp6);
         hashTab.add(emp7);
         hashTab.list();
         System.out.println("=================");
         hashTab.findEmpById(7);
         System.out.println("---------------------");
         hashTab.delById(8);
         hashTab.list();
    }
}


class Emp{
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\''+"}";
    }
}

class HashTab{
    private int size;
    private EmpLinkedList[] empLinkedLists;

    public HashTab(int size) {
        this.size = size;
        empLinkedLists = new EmpLinkedList[size];

        for (int i = 0; i < size; i++) {
            empLinkedLists[i] = new EmpLinkedList();
        }
    }

    //添加员工
    public void add(Emp emp){
        int empLinkedListNo = hashFun(emp.id);
        //将emp添加到对应的链表
        empLinkedLists[empLinkedListNo].add(emp);
    }
    public void list(){
        for (int i = 0; i < size; i++) {
            empLinkedLists[i].list();
        }
    }
    //散列函数、取模
    public int hashFun(int id){
        return id % size;
    }

    //查找id findEmpById
    public void findEmpById(int id){
        int hashFun = hashFun(id);
        Emp empById = empLinkedLists[hashFun].findEmpById(id);
        if(empById != null){
            System.out.println(empById);
        }else{
            System.out.println("没有查找到该员工");
        }
    }

    public void delById(int id){
        int hashFun = hashFun(id);
        //删除操作
        empLinkedLists[hashFun].delById(id);
    }
}


//链表
class EmpLinkedList{

    public Emp head;

    //加入
    public void add(Emp emp){
        //添加第一位
        if(head == null){
            head = emp;
            return;
        }
        Emp curEmp = head;
        while(true){
            if(curEmp.next == null){
                break;
            }
            curEmp = curEmp.next; //
        }
        //当前curEmp为最后一个
        curEmp.next = emp;
    }

    //遍历当前链表
    public void list(){
        if(head == null){
            System.out.println("当前列表为空");
            return ;
        }
        System.out.print("当前列表的信息为:==>   ");
        Emp curEmp = head;
        while (true){
            System.out.print(curEmp+"\t");
            if(curEmp.next == null){
                System.out.println();
                break;
            }
            curEmp = curEmp.next;  //后移 遍历
        }
    }
    //按照id来查找Emp
    public Emp findEmpById(int id){
        //判断链表判空
        if(head == null){
            System.out.println("找不到");
        }
        Emp curEmp = head;
        while(true){
            if(id == curEmp.id){
//                break; //找到
                return curEmp;
            }
            if(head.next == null){
//                System.out.println("找不到");
                return null;
            }
            head = head.next;
        }
    }

    //删除emp根据顾客id
    public void delById(int id){
        if(head == null){
            System.out.println("为空");
            return;
        }
        //是否为头节点。
        if(head.id == id){
            //删除头节点
            head = head.next;
            return ;
        }
        Emp curEmp = head;
        while (true){
            if(curEmp.next == null){
                System.out.println("不存在、删除失败");
            }
            //判断下一个节点是否是删除的节点
            if(curEmp.next.id == id){
                //删除
                System.out.println("删除成功");
                curEmp.next = curEmp.next.next;
                break;
            }
            curEmp = curEmp.next;
        }
    }
}


















